package org.ITschool.system.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.ITschool.entityOrview.Email;
import org.ITschool.support.util.AppcontextFactory;
import org.ITschool.system.service.EmailService;
import org.ITschool.system.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("email")
public class EmailController {
	 private EmailService emailService;
	 private UserService userService;
	   private Integer pagesize=25;
	   private void init(){
		   emailService=(EmailService)AppcontextFactory.getappContext().getBean("Email_Service");
		   userService=(UserService)AppcontextFactory.getappContext().getBean("User_Service");
	   }
	   @RequestMapping("/send_email")
	   public void send_email(HttpServletRequest request,HttpServletResponse response,HttpSession session,Email email){
		 init();
		 try {
			emailService.send_email(request, response, session, email, "");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	   }
	   @RequestMapping("/band_email")
	   public void band_email(HttpServletRequest request,HttpServletResponse response,HttpSession session,String email_password){
		   init();
		   userService.band_emailservice(request, response, session, email_password, "");
	   }
	   @RequestMapping("/email_list")
	   public String email_list(HttpServletRequest request,HttpServletResponse response,HttpSession session,String receive_name,String send_time,Integer pageNum){
		  init();
		  Email email=null;
		   if(pageNum==null)pageNum=1;
		 
		   if(receive_name!=null&&send_time!=null){
			   Email email_1=new Email();
			   email_1.setReceive_name(receive_name);
			   if(!send_time.equals(""))
			   email_1.setSend_time(send_time+"%");
			   else {
				email_1.setSend_time("");
			}
			   email=email_1;
		   }
		   return emailService.email_list(request, response, session, email, "system/email_list", pageNum, pagesize);
	   }
	   @RequestMapping("del_email-{id}")
	   public void del_email(HttpServletRequest request,HttpServletResponse response,HttpSession session,@PathVariable("id") Integer id){
		   init();
		   emailService.delemail_id(request, response, session, id, "");
	   }
	   @RequestMapping("email_detail-{id}")
	   public String email_detail(HttpServletRequest request,HttpServletResponse response,HttpSession session,@PathVariable("id") Integer id){
		   return emailService.email_detail(request, response, session, id, "system/email_detail");
	   }
	   @RequestMapping("send_email_s")
	   public void send_email_s(HttpServletRequest request,HttpServletResponse response,HttpSession session,Email email){
		   init();
			 try {
				emailService.send_email_s(request, response, session, email, "");
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	   }
	   
}
